#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    loadConfig();
    dbUtil = new DBUtil(config->getDBInfo());
    if(!dbUtil->connect())
    {
        QMessageBox::information(this,"error","connect database error",QMessageBox::Yes);
        exit(1);
    }
    QList<QMap<QString,QVariant>> list = dbUtil->query("pk20171013","HYDM='rb1801'");
    showCandle(list);
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::loadConfig()
{
    config = new Config();
}

void MainWindow::showCandle(QList<QMap<QString,QVariant>> list)
{
    QCustomPlot *customPlot = new QCustomPlot(this);
    this->setCentralWidget(customPlot);
    customPlot->legend->setVisible(true);

    // generate two sets of random walk data (one for candlestick and one for ohlc chart):
    int n = 500;
    QVector<double> time(n), value1(n), value2(n);
    QDateTime start = QDateTime(QDate::currentDate());
    start.setTimeSpec(Qt::UTC);
    double startTime = start.toTime_t();
    double binSize = 3600; // bin data in 1 day intervals
    time[0] = startTime;
    value1[0] = 60;
    value2[0] = 20;
    qsrand(9);
    for (int i=1; i<n; ++i)
    {
      time[i] = startTime + 3600*i;
      value1[i] = value1[i-1] + (qrand()/(double)RAND_MAX-0.5)*10000;
      value2[i] = value2[i-1] + (qrand()/(double)RAND_MAX-0.5)*3;
    }

    // create candlestick chart:
    QCPFinancial *candlesticks = new QCPFinancial(customPlot->xAxis, customPlot->yAxis);
    candlesticks->setName("Candlestick");
    candlesticks->setChartStyle(QCPFinancial::csCandlestick);
    candlesticks->data()->set(QCPFinancial::timeSeriesToOhlc(time, value1, binSize, startTime));
    candlesticks->setWidth(binSize*0.9);
    candlesticks->setTwoColored(true);
    candlesticks->setBrushPositive(QColor(0, 255, 0));
    candlesticks->setBrushNegative(QColor(255, 0, 0));
    candlesticks->setPenPositive(QPen(QColor(0, 0, 0)));
    candlesticks->setPenNegative(QPen(QColor(0, 0, 0)));
    // configure axes of both main and bottom axis rect:
    QSharedPointer<QCPAxisTickerDateTime> dateTimeTicker(new QCPAxisTickerDateTime);
    dateTimeTicker->setDateTimeSpec(Qt::UTC);
    dateTimeTicker->setDateTimeFormat("yy-MM-dd");
//    customPlot->xAxis->setBasePen(Qt::NoPen);
    customPlot->xAxis->setTickLabels(true);
    customPlot->xAxis->setTicks(true); // only want vertical grid in main axis rect, so hide xAxis backbone, ticks, and labels
    customPlot->xAxis->setTicker(dateTimeTicker);
    customPlot->rescaleAxes();
    customPlot->xAxis->scaleRange(1.025, customPlot->xAxis->range().center());
    customPlot->yAxis->scaleRange(1.1, customPlot->yAxis->range().center());

    // make axis rects' left side line up:
    QCPMarginGroup *group = new QCPMarginGroup(customPlot);
    customPlot->axisRect()->setMarginGroup(QCP::msLeft|QCP::msRight, group);
}

QList<StockData> MainWindow::processData(QList<QMap<QString, QVariant> > list)
{
    QList<StockData> ls;
    int count = list.count();
    for(int i=0;i<list.count();i++)
    {
        StockData sd;
        QMap<QString,QVariant> map = list.at(i);
        sd.low = sd.high = sd.open = sd.close = map.value("ZXJ").toDouble();
        sd.time = QDateTime::fromString(map.value("SJ").toString().mid(0,19),"yyyy-MM-dd hh:mm:ss").toMSecsSinceEpoch();;
        i++;
        while(i<count)
        {
            QMap<QString,QVariant> mm = list.at(i);
            qint64 time =  QDateTime::fromString(mm.value("SJ").toString().mid(0,19),"yyyy-MM-dd hh:mm:ss").toMSecsSinceEpoch();
            if(sd.time==time)
            {
                double temp = mm.value("ZXJ").toDouble();
                if(temp>sd.high)sd.high = temp;
                if(temp<sd.low)sd.low = temp;
                sd.close = temp;
                i++;
            }
            else
            {
                ls.append(sd);
                i--;
                break;
            }
        }
        if(i==count)ls.append(sd);
    }
    return ls;
}

